﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
using PlanningGIS.Framework;
using PlanningGIS.Framework.Controls;

namespace PlanningGIS.Statistics
{
    internal  class SpecifyStatButton:BaseDrawPolygonTool
    {
        private IFeatureLayer pFeatureLayer;
        IApplication m_Application = null;
        private StatLayer m_pStatLyr = null;
        IMapControl3 pMapControl = null;

        public SpecifyStatButton()
        {
            base.m_caption = "专题统计";
            base.m_toolTip = base.m_caption;
        }

        public override void OnCreate(object hook)
        {
            m_Application = hook as IApplication;
            base.OnCreate(hook);
        }

        public override bool Enabled
        {
            get
            {
                
                if ((m_Application as IFramework).IsMapCtrlactive)
                {
                    IEnumLayer layers = (m_Application.Document as IMxDocument).ActiveView.FocusMap.get_Layers(null, true);
                    ILayer layer = layers.Next();
                    while (layer != null)
                    {
                        if (layer is IFeatureLayer)
                        {
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                            return true;
                        }
                        layer = layers.Next();
                    }
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(layers);
                }
                return false;
            }
        }

        public override int Bitmap
        {
            get
            {
                string bitmapResourceName = "PlanningGIS.Statistics.Res." + this.GetType().Name + ".png";
                System.IO.Stream s = GetType().Assembly.GetManifestResourceStream(bitmapResourceName);
                if (s == null) return 0;
                return new Bitmap(s).GetHbitmap().ToInt32();
            }
        }

        public override void OnClick()
        {
            //base.OnClick();

            if ((m_Application as IFramework).IsMapCtrlactive)
                pMapControl = (m_Application as IFramework).MapControl;
            else
                return;

            if (pMapControl == null || pMapControl.Map.LayerCount == 0) return;

            FrmSpecifyStat statForm = new FrmSpecifyStat(m_Application, pMapControl);
            if (statForm.Visible == false)
            {
                statForm.Show((m_Application as IFramework).MdiForm);
            }
            //m_pStatLyr = statForm.ReportStatLayer;
            //statForm.ShowDialog();
            //if (statForm.DialogResult == DialogResult.OK)
            //{
            //    m_pStatLyr = statForm.ReportStatLayer;
            //    string name = statForm.ReportName;
            //    Hashtable pHash = null;

            //    if (name.Equals("植被统计扩展"))
            //    {
            //        FrmVegStatPara veg = new FrmVegStatPara();
            //        if (veg.ShowDialog() == DialogResult.OK)
            //        {
            //            pHash = veg.Parameters;
            //        }
            //    }
            //    if (m_pStatLyr == null)
            //    {
            //        ControlUtility.ShowInfomationMsgbox("要素统计为空！");
            //        return;
            //    }
            //    pFeatureLayer = CartoUtility.FindFeatureLayerByName(pMapControl.Map, m_pStatLyr.Value);
            //    if (pFeatureLayer == null)
            //    {
            //        ControlUtility.ShowInfomationMsgbox("尚未找到" + m_pStatLyr.Value + "层，请先加载该层！");
            //        return;
            //    }

            //    bool isSuccess;
            //    FeatureStat pStat = new FeatureStat();
            //    pStat.FeatureClass = pFeatureLayer.FeatureClass;
            //    pStat.MapControl = pMapControl;
            //    if (name.Equals("植被统计扩展"))
            //    {
            //        isSuccess = pStat.Stat(statForm.XZQGeometry, m_pStatLyr, statForm.ReportStatLayers, name, pHash);
            //    }
            //    else
            //    {
            //        isSuccess = pStat.Stat(statForm.XZQGeometry, m_pStatLyr,  name);
            //    }
            //}
        }


    }
}
